﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Data.Model;
using DotNet.Common.NHibernateUtil;
using NHibernate;
using NHibernate.Criterion;
using ZLCMS.Business.Util;

namespace ZLCMS.Web.Interface.weixin.Html
{
    public partial class WxMemberCardHtml : UrlRewritePage
    {
        protected WxMemberCardConfig _config { get; set; }
        protected WxMemberCard _membercard { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            string owner = Request["account"];
            string openId = Request["openid"];

            if (!string.IsNullOrEmpty(owner) && !string.IsNullOrEmpty(openId))
            {
                using (ISession session = SessionFactoryHelper.CreateSession())
                {
                    _config = session.CreateCriteria<WxMemberCardConfig>().Add(Expression.Eq("OwnerAccount", owner))
                        .UniqueResult<WxMemberCardConfig>();

                    _membercard = session.CreateCriteria<WxMemberCard>().Add(Expression.Eq("WxId", openId)).Add(Expression.Eq("OwnerAccount", owner)) .UniqueResult<WxMemberCard>();
                }
            }
            //判断是否有会员卡 有则显示信息 无则跳转到申请页
            if (_config != null)
            {
                if (_membercard == null)
                {
                    //判断是否需要完善个人信息
                    if (_config.NeedConfig == 1)
                    {
                        Response.Redirect("/Interface/weixin/Html/WxCompleteMemberInfoHtml.aspx?url=membercard&account=" + owner + "&openid=" + openId);
                    }
                    else if (_config.NeedConfig == 0)//直接跳转到申请页
                    {
                        Response.Redirect("/Interface/weixin/Html/WxApplyMemberCardHtml.aspx?account=" + owner + "&openid=" + openId);
                    }
                }
            }
            if (_config == null)
            {
                _config = new WxMemberCardConfig();
            }
            if (_membercard == null)
            {
                _membercard = new WxMemberCard();
                _membercard.CardNo = "未绑定会员卡";

            }
        }
    }
}